<?php

namespace Biz\Services\User;

use Lumen\Logger\Logger;
use Illuminate\Http\Request;
use Biz\Models\User\UserClient;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Biz\Constants\User\UserConstants;
use Lumen\Core\Contracts\ExceptionContract;
use Lumen\Core\Exceptions\BusinessException;
use Biz\Repositories\User\UserClientRepository;

/**
 * UserClientService.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class UserClientService
{
    /**
     * @var UserClientRepository
     */
    protected $repository;

    /**
     * @var UserService
     */
    protected $userService;

    /**
     * RepositoryController constructor.
     *
     * @param UserClientRepository $repository
     * @param UserService          $userService
     */
    public function __construct(UserClientRepository $repository, UserService $userService)
    {
        $this->repository  = $repository;
        $this->userService = $userService;
    }

    /**
     * Store a new client.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return UserClient
     */
    public function store(Request $request): UserClient
    {
        $data = $request->all();

        return $this->create($data);
    }

    /**
     * 创建新客户.
     *
     * @param array $data
     *
     * @throws \Exception
     * @throws ExceptionContract
     *
     * @return UserClient
     */
    public function create(array $data): UserClient
    {
        // 1) Start Transaction
        DB::beginTransaction();
        try {
            // 创建用户
            $data['type'] = UserConstants::TYPE_CLIENT;
            $user         = $this->userService->create($data);

            // 创建客户
            $data['user_id']            = $user->id;
            $allowField                 = $this->repository->createModel()->getFillable();
            $data                       = array_only($data, $allowField);
            /** @var UserClient $userClient */
            $userClient = $this->repository->create($data);
            if (! $userClient) {
                throw new BusinessException(trans('user.client_create_fail'));
            }

            $userClient->setUser($user);

            // 2) Commit Transaction
            DB::commit();
        } catch (\Exception $e) {
            // 3) Rollback Transaction
            DB::rollback();
            // 4) Log Transaction
            Logger::error(__CLASS__, __FUNCTION__, $e);
            // 5) Throw Exception
            throw $e;
        }

        return $userClient;
    }

    /**
     * Find.
     *
     * @param $id
     *
     * @throws BusinessException
     *
     * @return \Biz\Models\User\UserClient
     */
    public function find($id): UserClient
    {
        $userClient = $this->repository->find($id);
        if (is_null($userClient)) {
            throw new BusinessException(trans('user.client_not_found'));
        }

        return $userClient;
    }

    /**
     * Login.
     *
     * @param array $data
     *
     * @throws \Exception
     * @throws BusinessException
     *
     * @return UserClient
     */
    public function login(array $data): UserClient
    {
        $user       = $this->userService->login($data);
        $userClient = $user->client()->first();

        if (is_null($userClient)) {
            throw new BusinessException(trans('user.client_not_found'));
        }

        $userClient->setUser($user);

        return $userClient;
    }
}
